package com.example.park2212.emp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_base.entity.DepartBean
import com.example.lib_base.entity.DepartBeanItem
import com.example.lib_base.entity.DepartNumBean
import com.example.lib_base.mvvm.BaseActivity
import com.example.park2212.R
import com.example.park2212.databinding.ActivityEmpBinding

import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/app/emp/EmpActivity")
class EmpActivity : BaseActivity<ActivityEmpBinding,EmpViewModel>() {
    val empAdapter by lazy { EmpAdapter() }
    val departs = mutableListOf<DepartBeanItem>()
    var departNums = mutableListOf<DepartNumBean>()
    val empNumAdapter by lazy { EmpNumAdapter() }
    override fun initView() {
        binding.empRv.apply {
            adapter = empAdapter
            layoutManager = LinearLayoutManager(this@EmpActivity)
        }

        binding.empRvDepark.apply {
            adapter = empNumAdapter
            layoutManager = LinearLayoutManager(this@EmpActivity,RecyclerView.HORIZONTAL,false)
        }

        binding.headEmp.apply {
            headerBack.setOnClickListener {
                finish()
            }
            headerTitle.setText("人员管理")
            headerAdd.visibility = View.GONE
        }
        binding.btnAddemp.setOnClickListener {
            ARouter.getInstance().build("/app/emp/EmpAddActivity").navigation()
        }
    }

    override fun initData() {
       viewModel.getEmpsSuccess.observe(this){
           empAdapter.data.clear()
           empNumAdapter.data.clear()
           it.forEach {emp->
               departs.forEach {
                   if (it.parent == emp.departmentId){
                       emp.departName = it.name
                   }
               }
           }
           empAdapter.data.addAll(it)
           empAdapter.notifyDataSetChanged()

//           //统计每个部门有多少人
           val map = empAdapter.data.filter {
               it.departName != null }.groupBy {
               it.departName!! }

           //Log.i("sss",map.toString())
           departs.clear()
           map.forEach { s, empBeanItems ->
               departNums.add(DepartNumBean(s,empBeanItems.size))
               //Log.i("sss","$s,$empBeanItems")
           }
           empNumAdapter.addData(departNums)
           empNumAdapter.notifyDataSetChanged()


       }
        viewModel.getDepartSuccess.observe(this){
            departs.clear()
            empNumAdapter.data.clear()
            empAdapter.data.clear()
            departs.addAll(it)
            viewModel.getEmp()
        }
    }

    override fun onResume() {
        super.onResume()
        //viewModel.getEmp()
        viewModel.getDepart()
    }

}